from django.db import models
from django.contrib.auth.models import User


# Create your models here.


class Classify(models.Model):
    father_list = models.ForeignKey('self', verbose_name="上级", on_delete=models.CASCADE, blank=True, null=True)

    class Meta:
        abstract = True


class Child(Classify):
    child_list = models.CharField(verbose_name="子目录", max_length=64, unique=True, blank=True, null=True)

    def __str__(self):
        return u'%s' % self.child_list if self.father_list is None else u'%s %s ' % (self.father_list, self.child_list)

    class Meta:
        verbose_name = "目录"
        verbose_name_plural = "目录"


class Article(models.Model):
    title = models.CharField("文章标题", max_length=1024)
    author = models.ForeignKey(User, verbose_name="文章作者", on_delete=models.CASCADE, blank=True, null=True)
    publish_time = models.DateTimeField("发布时间", auto_now_add=True, blank=True, null=True)
    content = models.TextField(verbose_name="文章正文")
    classify = models.ForeignKey(Child, verbose_name="所属目录", on_delete=models.CASCADE)

    def __str__(self):
        return u'%s %s %s %s %s' % (self.title, self.author, self.publish_time, self.content, self.classify)

    class Meta:
        verbose_name = "文章"
        verbose_name_plural = "文章"


class Comment(models.Model):
    comment_userlink = models.ForeignKey(User, verbose_name="当前登录用户", on_delete=models.CASCADE)
    comment_time = models.DateTimeField("评论时间", auto_now_add=True)
    comment_content = models.TextField(verbose_name="评论内容")
    link_to_article = models.ForeignKey(Article, on_delete=models.CASCADE, blank=True, null=True)

    def __str__(self):
        return u'%s %s %s' % (self.comment_userlink, self.comment_time, self.comment_content)

    class Meta:
        verbose_name = "评论"
        verbose_name_plural = "评论"


class Edit(models.Model):
    edit_userlink = models.ForeignKey(User, verbose_name="当前登录用户", on_delete=models.CASCADE)
    edit_time = models.DateTimeField("编辑时间", auto_now=True)
    remark = models.CharField("备注", max_length=512)
    link_to_article = models.ForeignKey(Article, on_delete=models.CASCADE, blank=True, null=True)

    # 备注，如果改动文章变动较大，大概写下文本差异吧

    def __str__(self):
        return u'%s %s %s' % (self.edit_userlink, self.edit_time, self.remark)

    class Meta:
        verbose_name = "编辑"
        verbose_name_plural = "编辑"
